package com.newsproject.service.impl;

import Util.FormatTime;
import com.newsproject.mapper.NewsMapper;
import com.newsproject.mapper.UserMapper;
import com.newsproject.pojo.DTO.OtherUserDTO;
import com.newsproject.pojo.DTO.PostDTO;
import com.newsproject.pojo.Result;
import com.newsproject.pojo.User;
import com.newsproject.service.OtherPersonService;


import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.ui.Model;


import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Slf4j
@Service
public class OtherPersonServiceImpl implements OtherPersonService {
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private NewsMapper newsMapper;
    @Override
    public String otherPersonMainPage(Integer otherUserId, HttpSession session, Model model) {
        //判断是否关注了该用户
        User userInfo = (User)session.getAttribute("userInfo");
        int userId=0;
        if(userInfo != null){
            userId=userInfo.getUserId();
        }
        //查询他人信息
        OtherUserDTO user = userMapper.findOtherUserById(userId, otherUserId);
        //查询他人文章
        List<PostDTO> posts = newsMapper.selectPostByPostTypeAndUserId(otherUserId,0,0);
        //格式化时间
        posts.forEach(post->{
            String createdTime = FormatTime.formatTimeDifference(post.getTime());
            post.setCreatedTime(createdTime);
        });

        model.addAttribute("posts",posts);
        model.addAttribute ("userInfo",user);

        return "othersHomePage";
    }

    @Override
    public Result<Map<String,Object>> findOtherFollowers(Integer otherUserId, Integer pageNum, HttpSession session) {
        //获取当前登录用户的id
        User userInfo = (User)session.getAttribute("userInfo");
        //当前用户id， 默认是0， 表示未登录
        int userId = 0;
        //如果用户已经登录， 则获取用户id
        if(userInfo != null){
            userId = userInfo.getUserId();
        }
        Result<Map<String,Object>> result = new Result<>();
        Map<String,Object> response = new HashMap<>();
        //如果pageNum为空或者小于1， 则设置为1
        if(pageNum == null || pageNum < 1){
            pageNum = 1;
        }
        //获取分页的起始索引
        int startIndex = (pageNum -1 ) * 10;
        //查询关注者列表
        List<OtherUserDTO> followers = userMapper.selectFollowerList(userId, otherUserId, startIndex);
        Integer totalCount = userMapper.countFollower(otherUserId);
        if(totalCount == 0) {
           response.put("message", "暂时没有粉丝");
           result.setData(response);
           result.setCode(200);
           return result;
        }
        boolean hasMore = startIndex+ 10 < totalCount;
        response.put("followers", followers);
        response.put("totalCount", totalCount);
        response.put("hasMore", hasMore);
        result.setData(response);

        return result;
    }

    @Override
    public Result<Map<String,Object>> findOtherFollowed(Integer otherUserId, Integer pageNum, HttpSession session) {
        User userInfo = (User)session.getAttribute("userInfo");
        int userId  =0;
        if(userInfo != null){
            userId = userInfo.getUserId();
        }
        Result<Map<String,Object>> result = new Result<Map<String,Object>>();
        Map<String,Object> response = new HashMap<>();

        if(pageNum == null || pageNum < 1){
            pageNum = 1;
        }
        int startIndex = (pageNum -1 ) * 10 ;

        List<OtherUserDTO> followedList = userMapper.findFollowedList(userId, otherUserId, startIndex);
        Integer totalCount = userMapper.countFollowed(otherUserId);
        if(totalCount == 0) {
            response.put("message", "暂时没有关注");
            result.setData(response);
            result.setCode(200);
        }
        boolean hasMore = startIndex+ 10 < totalCount;
        response.put("followedList", followedList);
        response.put("totalCount", totalCount);
        response.put("hasMore", hasMore);
        result.setData(response);
        return result;
    }
}
